home *** CD-ROM | disk | FTP | other *** search
/ Compendium Deluxe 1 / LSD Compendium Deluxe 1.iso / a / programming / misc / srev142.lha / SetRev142 / setrev142.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-16  |  13.2 KB  |  400 lines

  1. /****************************************************************************
  2.  * This is SetRev, a BumpRev replacement completely  written in extended    *
  3.  * ANSI-C.                                                                  *
  4.  *                                                                          *
  5.  * Author:         Alexander Sieb                                              *
  6.  * Date:        09-Oct-1992                                                 *
  7.  * Source Name: SetRev.c                                                    *
  8.  * Object Name: SetRev                                                      *
  9.  * Kickstart:   3.0 (39.106)                                                *
  10.  * Workbench:   3.0 (39.39)                                                 *
  11.  * Copyright:   1992, 1993 by Alexander Sieb                                *
  12.  * License:                                                                 *
  13.  *   This program is free software; you can redistribute it and/or modify   *
  14.  *   it under the terms of the GNU General Public License as published by   *
  15.  *   the Free Software Foundation; either version 2 of the License, or      *
  16.  *   (at your option) any later version.                                    *
  17.  *                                                                          *
  18.  *    This program is distributed in the hope that it will be useful,       *
  19.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of         *
  20.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
  21.  *   GNU General Public License for more details.                           *
  22.  *                                                                          *
  23.  *   You should have received a copy of the GNU General Public License      *
  24.  *   along with this program; if not, write to the Free Software            *
  25.  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              *
  26.  *                                                                          *
  27.  *   For any help or bug reports contact the author by paper mail           *
  28.  *                                                                          *
  29.  *        Alexander Sieb                                                    *
  30.  *        Karlstr. 5                                                        *
  31.  *        71638 Ludwigsburg                                                 *
  32.  *        Federal Republic of Germany                                       *
  33.  *                                                                          *
  34.  *                                                                          *
  35.  * PROGRAM HISTORY:                                                         *
  36.  *                                                                          *
  37.  *  Date         Person      Action                                         *
  38.  * ----------- ----------  ----------                                       *
  39.  *  09-Oct-92    A. Sieb     First Release v1.01                            *
  40.  *  18-Oct-92    A. Sieb     Fully rewritten. Now using DOS intern buffered *
  41.  *                           IO.                                            *
  42.  *                           Several minor bugs have been fixed             *
  43.  *  18-Oct-92    A. Sieb     Second Release v1.34                           *
  44.  *  25-Oct-92    A. Sieb     QUIET option added                             *
  45.  *                           MAJOR CHANGE: Version is no longer optional    *
  46.  *                           All standard IO moved to main loop             *
  47.  *  26-Oct-92    A. Sieb     DEPendency option added. Revision will only be *
  48.  *                           increased if RevFile is older or has the same  *
  49.  *                           date than the by DEP specified file            *
  50.  *                           Third Release v1.36                            *
  51.  *  17-Nov-92    A. Sieb     SHOWREV option added for BumpRev compatibility *
  52.  *                           Error prints will not be affected by QUIET or  *
  53.  *                           SHOWREV option.                                *
  54.  *                           QUIET option overrides SHOWREV option          *
  55.  *                           Fourth Release v1.37                           *
  56.  *  23-Nov-92    A. Sieb     NoC and NoAsm options added.                   *
  57.  *                           Fifth Release v1.38                            *
  58.  *  16-May-93    A. Sieb     Sixth Release v1.39                            *
  59.  *                           VSTRING2 added for use with none ESC String    *
  60.  *  01-Aug-93    A. Sieb     Seventh Release v1.40                          *
  61.  *                           ANSI C file genration added.                   *
  62.  *                           TIME added for creation time output            *
  63.  *  12-Sep-93    A. Sieb     Eighth Release v1.41                           *
  64.  *                           VersionString identifier added to ANSI C file  *
  65.  *                           generation.                                    *
  66.  *                                                                          *
  67.  * NOTES:                                                                   *
  68.  *        Program will only run under Kickstart 37.175 and higher           *
  69.  *        Don't compile this piece of code with resident startup, because   *
  70.  *        no used global data is cleared.                                   *
  71.  ****************************************************************************/
  72.  
  73.  /* INCLUDEs */
  74.  
  75. #include <exec/types.h>
  76. #include <proto/dos.h>
  77. #include <dos/rdargs.h>
  78. #include <dos/datetime.h>
  79. #include <stdlib.h>
  80. #include <string.h>
  81.  
  82. /* DEFINEs */
  83.  
  84. #define EOF (-1)
  85.  
  86. #define VERSION "1.42"
  87. #define PRGNAME "SetRev"
  88. #define DATE "16.01.94"
  89. /* GLOBALs */
  90.  
  91. const UBYTE VerTag[] = {"\0$VER: "PRGNAME" "VERSION" ("DATE")"};
  92.  
  93. UBYTE ObjFile[33];
  94. UBYTE RevFile[33];
  95. UBYTE C_Header[33];
  96. UBYTE ASM_Header[33]; 
  97. UBYTE ANSI_C_File[33];
  98.  
  99. LONG Revision;
  100. LONG Version;
  101.  
  102. struct DateTime Date;
  103. UBYTE DateString[9];
  104. UBYTE TimeString[9];
  105.  
  106. struct RDArgs *ShellArgs;
  107. STRPTR Template = {"REV/N/K,OBJ/K,QUIET/S,DEP/K,SHOWREV/S,NOASM/S,NOC/S,NOANSI/S,VER/N/A,FILE/A"};
  108. LONG ArgArray[10];
  109. LONG **Args;
  110. enum Arguments { revision, object, quiet, dependency, showrev, noasm, noc, noansi,  version, file };
  111.  
  112. /*
  113.  * CompareFileDate().
  114.  * -------------
  115.  * Compare two file creation datestamp for DEPendency option.
  116.  * Returns <0 if c1 is later than c2, 0 if they are equal, >0 if c2 is later
  117.  * c1 than.
  118.  */
  119.  
  120. LONG CompareFileDate(char *c1, char *c2)
  121. {
  122.     BPTR l1, l2;
  123.     struct FileInfoBlock *fib1, *fib2;
  124.     LONG res = NULL;
  125.  
  126.     if(l1 = Lock(c1, SHARED_LOCK)) {
  127.         if(l2 = Lock(c2, SHARED_LOCK)) {
  128.             if(fib1 = AllocDosObject(DOS_FIB, NULL)) {
  129.                 if(fib2 = AllocDosObject(DOS_FIB, NULL)) {
  130.                     if(Examine(l1, fib1)) {
  131.                         if(Examine(l2, fib2))
  132.                             res = CompareDates(&fib1->fib_Date, &fib2->fib_Date);
  133.                     }
  134.                     FreeDosObject(DOS_FIB, fib2);
  135.                 }
  136.                 FreeDosObject(DOS_FIB, fib1);
  137.             }
  138.             UnLock(l2);
  139.         }
  140.         UnLock(l1);
  141.     }
  142.     return(res);
  143. }
  144.  
  145. /*
  146.  * GetRevision().
  147.  * -------------
  148.  * Reads current revision number from specified file.
  149.  */
  150.  
  151. LONG GetRevision(UBYTE *c)
  152. {
  153.     UBYTE buf[11] = {"\0"};
  154.     UBYTE b = 0;
  155.     UWORD i = 0;
  156.     BPTR f;
  157.     LONG rev = 0;
  158.  
  159.     if(f = Open(c,MODE_OLDFILE)) {
  160.         while(i<11 && (b = FGetC(f))!=EOF)
  161.             buf[i++] = b;
  162.         Close(f);
  163.         rev = atol(buf);
  164.     }
  165.     return(++rev);
  166. }
  167.  
  168. /*
  169.  * WriteRevision().
  170.  * ---------------
  171.  * Write revision as non-terminated ASCII string to file and output terminal.
  172.  */
  173.  
  174. BOOL WriteRevision(UBYTE *c)
  175. {
  176.     BPTR f;
  177.  
  178.     if(f = Open(c, MODE_NEWFILE)) {
  179.         FPrintf(f,"%ld", Revision);
  180.         Close(f);
  181.         return(TRUE);
  182.     }
  183.     else
  184.         return(FALSE);
  185. }
  186.  
  187. /*
  188.  * WriteC().
  189.  * --------
  190.  * Writes the C header file.
  191.  */
  192.  
  193. BOOL WriteC(void)
  194. {
  195.     BPTR f;
  196.  
  197.     if(f = Open(C_Header, MODE_NEWFILE)) {
  198.         FPrintf(f,"/* C Headerfile created by "PRGNAME" "VERSION" on %ls at %ls */\n\n", DateString, TimeString);
  199.         FPrintf(f,"#define VERSION %ld\n", Version);
  200.         FPrintf(f,"#define REVISION %ld\n", Revision);
  201.         FPrintf(f,"#define DATE %lc%ls%lc\n", '"', DateString, '"');
  202.         FPrintf(f,"#define TIME %lc%ls%lc\n", '"', TimeString, '"');
  203.         FPrintf(f,"#define VERS %lc%ls %ld.%ld%lc\n", '"', ObjFile, Version, Revision,'"');
  204.         FPrintf(f,"#define VSTRING %lc%ls %ld.%ld (%ls)\\n\\r%lc\n", '"', ObjFile, Version, Revision, DateString,'"');
  205.         FPrintf(f,"#define VSTRING2 %lc%ls %ld.%ld (%ls)%lc\n", '"', ObjFile, Version, Revision, DateString,'"');
  206.         FPrintf(f,"#define VERSTAG %lc\\0$V",'"');
  207.         FPrintf(f,"ER: %ls %ld.%ld (%ls)%lc\n", ObjFile, Version, Revision, DateString,'"');
  208.         Close(f);
  209.         return(TRUE);
  210.     }
  211.     else
  212.         return(FALSE);
  213. }
  214.  
  215. /*
  216.  * WriteASM().
  217.  * --------
  218.  * Writes the ASM header file.
  219.  */
  220.  
  221. BOOL WriteASM(void)
  222. {
  223.     BPTR f;
  224.  
  225.     if(f = Open(ASM_Header, MODE_NEWFILE)) {
  226.         FPrintf(f,"; ASM Headerfile created by "PRGNAME" "VERSION" on %ls at %ls\n\n", DateString, TimeString);
  227.         FPrintf(f,"VERSION\t\tEQU\t%ld\n", Version);
  228.         FPrintf(f,"REVISION\tEQU\t%ld\n", Revision);
  229.         FPrintf(f,"DATE\tMACRO\n\t\tdc.b\t'%ls'\n\tENDM\n", DateString);
  230.         FPrintf(f,"TIME\tMACRO\n\t\tdc.b\t'%ls'\n\tENDM\n", TimeString);
  231.         FPrintf(f,"VERS\tMACRO\n\t\tdc.b\t'%ls %ld.%ld'\n\tENDM\n", ObjFile, Version, Revision);
  232.         FPrintf(f,"VSTRING\tMACRO\n\t\tdc.b\t'%ls %ld.%ld (%ls)',13,10,0\n\tENDM\n", ObjFile, Version, Revision, DateString);
  233.         FPrintf(f,"VSTRING2\tMACRO\n\t\tdc.b\t'%ls %ld.%ld (%ls)',0\n\tENDM\n", ObjFile, Version, Revision, DateString);
  234.         FPrintf(f,"VERSTAG\tMACRO\n\t\tdc.b\t0,'$VE",NULL);
  235.         FPrintf(f,"R: %ls %ld.%ld (%ls)',0\n\tENDM\n", ObjFile, Version, Revision, DateString);
  236.         Close(f);
  237.         return(TRUE);
  238.     }
  239.     else
  240.         return(FALSE);
  241. }
  242.  
  243. /*
  244.  * WriteANSI_C().
  245.  * -------------
  246.  * Writes an ANSI C source file.
  247.  */
  248.  
  249. BOOL WriteANSI_C(void)
  250. {
  251.     BPTR f;
  252.  
  253.     if(f = Open(ANSI_C_File, MODE_NEWFILE)) {
  254.         FPrintf(f,"/* ANSI C file created by "PRGNAME" "VERSION" on %ls at %ls */\n\n",DateString, TimeString);
  255.         FPrintf(f,"const unsigned long Version = %ld;\n", Version);
  256.         FPrintf(f,"const unsigned long Revision = %ld;\n", Revision);
  257.         FPrintf(f,"const unsigned char CreationDate[] = {%lc%ls%lc};\n", '"', DateString, '"');
  258.         FPrintf(f,"const unsigned char CreationTime[] = {%lc%ls%lc};\n", '"', TimeString, '"');
  259.         FPrintf(f,"const unsigned char VersionString[] = {%lc%ls %ld.%ld (%ls)%lc};\n", '"', ObjFile, Version, Revision, DateString,'"');
  260.         FPrintf(f,"const unsigned char VersionTag[] = {%lc\\0$V",'"');
  261.         FPrintf(f,"ER: %ls %ld.%ld (%ls)%lc};\n", ObjFile, Version, Revision, DateString,'"');
  262.         Close(f);
  263.         return(TRUE);
  264.     }
  265.     else
  266.         return(FALSE);
  267. }
  268.  
  269.  
  270. /*        
  271.  * _Main().
  272.  * ------
  273.  * Entry point of every C program. We don't call compilers' __main().
  274.  */
  275.  
  276. long main(char *c)
  277. {
  278.     LONG i;
  279.  
  280.     if(DOSBase->dl_lib.lib_Version < 37L)
  281.         return(20);
  282.  
  283. // Be aware of those ugly compilers which don't clear global data.
  284.  
  285.     memset(ArgArray, 0, sizeof(ArgArray));
  286.  
  287. // Read given inputs via ReadArgs DOS-call
  288.  
  289.     if(!(ShellArgs = ReadArgs(Template, ArgArray, NULL))) {
  290.         PrintFault(IoErr(), PRGNAME);
  291.     }
  292.     else {
  293.  
  294. // We have now the command line parsed. Go on generating the filenames.
  295.  
  296.         Args = (LONG **) ArgArray;
  297.  
  298.         if(!Args[quiet] && !Args[showrev])
  299.             Printf(""PRGNAME" v%s written and © by Alexander Sieb on "DATE".\n\n",VERSION);
  300.  
  301.         strcpy(RevFile, (char *)Args[file]);
  302.  
  303.         i=strlen(RevFile);
  304.         if(strcmpi(&RevFile[i-4], ".rev")) {
  305.             strcpy(C_Header, RevFile);
  306.             strcat(C_Header, ".h");
  307.             strcpy(ASM_Header, RevFile);
  308.             strcat(ASM_Header, ".i");
  309.             strcpy(ANSI_C_File, RevFile);
  310.             strcat(ANSI_C_File, ".c");
  311.             strcat(RevFile, ".rev");
  312.         }
  313.         else {
  314.             strncpy(C_Header, RevFile, (size_t)i-4);
  315.             strcpy(ASM_Header, C_Header);
  316.             strcpy(ANSI_C_File, C_Header);
  317.             strcat(C_Header, ".h");
  318.             strcat(ASM_Header, ".i");
  319.             strcat(ANSI_C_File, ".c");
  320.         }
  321.  
  322.         if(Args[dependency])
  323.             i = CompareFileDate(RevFile, (char *)Args[dependency]);
  324.         else
  325.             i = 0;
  326.         if(i<0) {
  327.             if(!Args[quiet])
  328.                 Printf("File %s is up to date.\n", RevFile);
  329.         }
  330.         else {
  331.  
  332. #ifdef DEBUG
  333.             Printf("Generating files %s, %s, %s, %s.\n", RevFile, C_Header, ASM_Header, ANSI_C_File);
  334. #endif
  335.  
  336. // We generate now the object name used in the header files
  337.  
  338.             if(Args[object])
  339.                 strcpy(ObjFile, (char *) Args[object]);
  340.             else {
  341.                 i = strlen(RevFile);
  342.                 strncpy(ObjFile, RevFile, (size_t)i-4);
  343.             }
  344.  
  345. // Now, we do the same with our revision
  346.  
  347.             Version = Args[version][0];
  348.  
  349.             if(!Args[revision])
  350.                 Revision = GetRevision(RevFile);
  351.             else
  352.                 Revision = Args[revision][0];
  353.  
  354. // Ok, so far so good. We save now our revision to the specified file 'RevFile'
  355.  
  356.             if(!WriteRevision(RevFile))
  357.                 PrintFault(IoErr(), PRGNAME);
  358.             else {
  359.  
  360. // Write revision to standard output terminal
  361.  
  362.                 if(!Args[quiet])
  363.                      Printf("Revision set to %ld.\n", Revision);
  364.  
  365. // Get local date and time
  366.  
  367.                 if(!DateStamp(&Date.dat_Stamp)) 
  368.                     PrintFault(IoErr(), PRGNAME);
  369.                 else {
  370.                     Date.dat_Format = FORMAT_CDN;
  371.                     Date.dat_StrDate = DateString;
  372.                     Date.dat_StrTime = TimeString;
  373.  
  374.                     if(!DateToStr(&Date))
  375.                         PrintFault(IoErr(), PRGNAME);
  376.                     else {
  377.                         for(i=0;i<strlen(DateString);i++)
  378.                             if(DateString[i] == '-')
  379.                                 DateString[i] = '.';
  380.  
  381. // We got todays date, now we write our header files.
  382.  
  383.                         if(!Args[noc])
  384.                             if(!WriteC())
  385.                                 PrintFault(IoErr(), PRGNAME);
  386.                         if(!Args[noasm])
  387.                             if(!WriteASM())
  388.                                 PrintFault(IoErr(), PRGNAME);
  389.                         if(!Args[noansi])
  390.                             if(!WriteANSI_C())
  391.                                 PrintFault(IoErr(), PRGNAME);
  392.                     }
  393.                 }
  394.             }        
  395.         }
  396.         FreeArgs(ShellArgs);
  397.     }
  398.     return(NULL);
  399. }
  400.